package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;

import java.io.*;

/**
 * 用来处理和用户相关的业务操作
 */
public class UserController {
    //
    private static String userDirName = "./users/";

    static {
        //程序加载时判断一下保存所有用户信息的目录是否存在，不存在先自动建出来
        File userDir = new File(userDirName);
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }


    /**
     * 用来处理用户注册操作
     *
     * @param request
     * @param response
     */
    public void reg(HttpRequest request, HttpResponse response) {
        System.out.println("开始处理用户注册....");
        //1.从request中获取用户表单上提交的注册信息
        //注：request.getParmeter()获取表单某输入框上的内容要传递的参数为该输入框name=“”属性指定
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
//        System.out.println("-------------------------------------------");
        System.out.println(username + "," + password + "," + nickname + "," + ageStr);
//        System.out.println("-------------------------------------------");
        //必要的验证工作，保证注册的四个信息不为空，否则只响应注册失败的提示页面
        //失败页面：reg_error.html居中显示一行自：注册失败，输入信息有误，请重新注册。
        if (username == null || password == null || nickname == null || ageStr == null
                || !(ageStr.matches("[0-9]+"))) {
            response.setEntity(new File("./webapps/myweb/reg_error.html"));
            return;
        }
        /*
            判断重复用户，如果是重复用户，则直接响应页面：rege_have_user.html
            该页面居中显示一行字：该用户已存在，请重新注册
         */
//        File dir = new File(userDirName);
//        FileFilter filter3 = (file)->{
//            return file.getName().endsWith(".obj");
//        };
//        File[] subs = filter3.listFiles();
//        for (int i = 0; i < subs.length; i++) {
//            File sub=subs[i];
//            if (username.equals(sub.getName())) {
//                response.setEntity(new File("./webapps/myweb/rege_have_user.html"));
//                return;
//            }
//        }

        File userFile =new File(userDirName+username+".obj");
        if (userFile.exists()){
            response.setEntity(new File("./webapps/myweb/rege_have_user.html"));
              return;
        }
        int age = Integer.parseInt(ageStr);
        //2.将该用户信息写入磁盘保存
        User user = new User(username, password, nickname, age);
        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        //./users/zhangsan.obj
                        new FileOutputStream(userDirName + username + ".obj")
                );
        ) {
            oos.writeObject(user);

            //注册成功
            response.setEntity(new File("./webapps/myweb/reg_success.html"));

        } catch (Exception e) {
            e.printStackTrace();
        }


        //3.设置response响应注册结果页面
        System.out.println("用户注册处理完毕!!!!");
    }
}
